
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% File:               first_stage.m
%
% Author:             Miguel R. Rueda and Sergio Ascencio
%
% Description:        It computes expected first stage probabilities 
% Language:           MATLAB R2013b (8.2.0.701) 64 Bit
%
% Related Reference:   "Representatives"
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function [phats,betas]=first_stage(Y,X,CLv_PAN,CLv_PRI)


%Extended
% varsx={'l_d_RC_others' 'l_margin' 'lcasillas' 'school_ic' 'l_diff_PRI_PAN' 'lpop' 'l_turnout' 'local' 'ldist_city' ...
% 'l_d_RC_others_sq' 'l_margin_sq' 'lcasillas_sq' 'school_ic_sq' 'l_diff_PRI_PAN_sq' 'lpop_sq' 'l_turnout_sq' 'ldist_city_sq' ...
% 'l_d_RC_others_l_margin' 'l_d_RC_others_lcasillas' 'l_d_RC_others_school_ic' 'l_d_RC_others_l_diff_PRI_PAN' 'l_d_RC_others_lpop' 'l_d_RC_others_l_turnout' 'l_d_RC_others_local' 'l_d_RC_others_ldist_city' ...
% 'l_margin_lcasillas' 'l_margin_school_ic' 'l_margin_l_diff_PRI_PAN' 'l_margin_lpop' 'l_margin_l_turnout' 'l_margin_local' 'l_margin_ldist_city' ...
% 'lcasillas_school_ic' 'lcasillas_l_diff_PRI_PAN' 'lcasillas_lpop' 'lcasillas_l_turnout' 'lcasillas_local' 'lcasillas_ldist_city' ...
% 'school_ic_l_diff_PRI_PAN' 'school_ic_lpop' 'school_ic_l_turnout' 'school_ic_local' 'school_ic_ldist_city' ...
% 'l_diff_PRI_PAN_lpop' 'l_diff_PRI_PAN_l_turnout' 'l_diff_PRI_PAN_local' 'l_diff_PRI_PAN_ldist_city' ...
% 'lpop_l_turnout' 'lpop_local' 'lpop_ldist_city' ...
% 'l_turnout_local' 'l_turnout_ldist_city' ...
% 'local_ldist_city' 'ldist_pri' 'gov_PRI' 'ldist_pan' 'gov_PAN'};


%Remember: when x variables change, code for Z_PRI, Z_PAN, and X need to be
%adjusted.

%Extended
%     Y_PRI=3-Y(:,1);
%     Y_PAN=3-Y(:,2);
%     Z_PAN=X(:,end);
%     Z_PRI=X(:,end-1);
%     X=X(:,1:end-2);

    Y_PRI=3-Y(:,1);
    Y_PAN=3-Y(:,2);
    Z_PAN=X(:,end-1:end);
    Z_PRI=X(:,end-3:end-2);
    X=X(:,1:end-4);

    betas=cell(1,2);
    phats=cell(1,2);
    
if nargin<3
        
    betas{1,1}=mnrfit([X Z_PRI],Y_PRI);
    phats{1,1}=mnrval(betas{1,1},[X Z_PRI]);

    betas{1,2}=mnrfit([X Z_PAN],Y_PAN);
    phats{1,2}=mnrval(betas{1,2},[X Z_PAN]);
    
else
   
    %State by state PAN
    
    CLv_u=unique(CLv_PAN);
    N_cl_PAN=length(CLv_u);
    phats_PAN=zeros(length(Y_PAN),3);

    
    for i=1:N_cl_PAN
        
        beta_PAN=mnrfit([X(CLv_PAN==CLv_u(i),:) Z_PAN(CLv_PAN==CLv_u(i),:)],Y_PAN(CLv_PAN==CLv_u(i),:));
        phats_PAN(CLv_PAN==CLv_u(i),:)=mnrval(beta_PAN,[X(CLv_PAN==CLv_u(i),:) Z_PAN(CLv_PAN==CLv_u(i),:)]);            
        
    end
    
    phats{1,2}=phats_PAN;

    %State by State PRI
    
    CLv_u=unique(CLv_PRI);
    N_cl_PRI=length(CLv_u);
    phats_PRI=zeros(length(Y_PRI),3);
    
    for i=1:N_cl_PRI
        
        beta_PRI=mnrfit([X(CLv_PRI==CLv_u(i),:) Z_PRI(CLv_PRI==CLv_u(i),:)],Y_PRI(CLv_PRI==CLv_u(i),:));
        phats_PRI(CLv_PRI==CLv_u(i),:)=mnrval(beta_PRI,[X(CLv_PRI==CLv_u(i),:) Z_PRI(CLv_PRI==CLv_u(i),:)]);    
               
    end
    
    phats{1,1}=phats_PRI;

    %Remember: phats column order: 1)full rep. prob, 2)medium rep. prob, 3)low rep. prob.
    %PRI probabilities in first cell and PAN in second
    
end
    
end




